package leetcode.editor.data;

import java.util.Scanner;

/**
 * 哈希表管理员工信息
 */
@SuppressWarnings("all")
public class HashTableDemo {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(5);

        //写一个Key
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("add: 添加雇员");
            System.out.println("list: 遍历雇员");
            System.out.println("exit: 退出系统");
            key =scanner.next();
            switch (key) {
                case "add":
                  System.out.println("输入id");
                  int id = scanner.nextInt();
                  System.out.println("输入名字");
                  String name = scanner.next();
                  //创建雇员
                  Employee employee = new Employee(id, name);
                  hashTable.add(employee);
                  break;
                case "list":
                    hashTable.list();
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }

    }
}
//创建HashTable用来管理多条链表
class  HashTable {
    //数组里面放的是链表
    public EmpLinkedList [] empLinkedLists;
    public int size;   //表示共有多少条

    public HashTable(int size) {
        //初始化链表大小
        empLinkedLists = new EmpLinkedList[size];
        this.size = size;
        //分别初始化每一条链表
        //TODO  重点 链表里面的每一个值都需要初始化
        for (int i = 0; i < size; i++) {
            empLinkedLists[i] = new EmpLinkedList();
        }
    }

    //添加雇员
    public void add(Employee employee) {
//        根据Id决定添加到那条链表
        int hashValue = hashFunction(employee.id);
        /**
         * empLinkedLists[hashValue] 这个是空
         */
        empLinkedLists[hashValue].add(employee);
    }

    //遍历哈希表
    public void list() {
        for (int i = 0; i < this.size; i++) {
            empLinkedLists[i].list(i);
        }
    }

    public int hashFunction(int id) {
        return id%size;
    }

}
//创建EmpLinkedList
class EmpLinkedList {

    //没有头指针--没有哨兵
    private Employee head ; //默认null

    //1.假定,当添加雇员的
    public void add(Employee employee) {
        //添加第一个雇员
        if (head == null) {
            head = employee;
            return;
        }
        Employee cur = head;
        while (cur.next!=null) {
            cur =cur.next;
        }
        cur.next = employee;
        return;
    }

    //遍历链表
    public void list(int no) {
        if (head==null) {
            System.out.println("第"+no+"条链表为空");
            return;
        }
        System.out.println("当前链表信息为");
        Employee cur = head;
        while (head!=null) {
            System.out.print("id:"+head.id+"   ");
            System.out.println("name:"+head.name);
            head = head.next;
        }
    }
}

class Employee {
    public int id;
    public String name;
    public Employee next;

    public Employee() {
    }

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
